/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "ui设置视口.h"
#include <UI/布局.h>

#include "A_引擎/文件工程/配置.h"



C_设置视口* C_设置视口::g当前操作2D编辑视口 = nullptr;

static uint32	g网格线绘制属性 = 0;


static bool on_设置视口按键(C_Widget* self, const S_键盘& ks) {
	
	return false;
}

static bool on_设置视口鼠标按下(C_Widget* self, const E_鼠标按钮& 按钮) {
	if (按钮 == E_鼠标按钮::e_鼠标中键) {
		
	}
	return false;
}

static bool on_设置视口鼠标放开(C_Widget* self, const E_鼠标按钮& 按钮) {
	if (按钮 == E_鼠标按钮::e_鼠标中键) {
		
	}
	return false;
}

static bool on_设置视口移动(C_Widget* self, const ivec4& value) {
	C_设置视口* box = (C_设置视口*)self;

	

	return false;
}


static void on_页面切换_Update(S_Props& prop) {
	C_设置视口* box = dynamic_cast<C_设置视口*>(prop.m_UI->m_部件->m_父部件);

	switch (f_prop_enum(box->m_设置类型)) {
		case 0: f_ui_滚动框安装视口组件(box->mui_滚动框, box->m_界面设置面板); break;
		case 1: f_ui_滚动框安装视口组件(box->mui_滚动框, box->m_操作设置面板); break;
		case 2: f_ui_滚动框安装视口组件(box->mui_滚动框, box->m_文件设置面板); break;
		case 3: f_ui_滚动框安装视口组件(box->mui_滚动框, box->m_编译设置面板); break;
	}
	
}


static bool on_视口鼠标滚轮(C_Widget* self, const S_鼠标& 鼠标) {
	if (self->m_父部件) {
		f_布局_扩展视口纵向滚动(self->m_父部件, self, C_Widget::g_鼠标.lZ * 40);
	}
	self->m_绘制属性更新 = true;

	return true;
}

static void on_视口滚动框布局(C_Widget* self, C_Widget** w, uint32 num) {
	//C_列表框* box = dynamic_cast<C_列表框*>(w[0]);
	//
	//uint32 itemNum = box->m_列表数据.size();
	//box->f_setH(itemNum * box->m_行高度);
	//box->f_setW(self->m_Size.x);
	//box->f_setSize(self->m_Size);

	//f_布局_扩展视口纵向滚动(self, w[0], C_Widget::g_鼠标.lZ * 40);
}

static void on_设置视口布局(C_Widget* self, C_Widget** w, uint32 num) {
	//f_Widget_计算视口缩放保持偏移比例(self);
	f_VBOX布局({1,1}, self->m_Size, w, num);
}

static void on_设置视口绘制(C_Widget* self, S_2D画布* 画布) {
	C_设置视口* box = (C_设置视口*)self;

	box->m_背景绘制.m_属性 = f_vg_gen属性(画布);

	S_凸边图元 图元 = {};
	图元.属性 = f_vg_get属性指针(画布, box->m_背景绘制.m_属性);
	box->m_背景绘制.f_Draw(画布, 图元, E_笔刷类型::e_单色填充);
	图元 = {};
	图元.属性 = f_vg_get属性指针(画布, box->m_背景绘制.m_属性);
	box->m_背景线绘制.f_Draw(画布, 图元, E_笔刷类型::e_单色描边);

	box->m_是否未绘制 = false;
}

static void on_设置视口变换修改(C_Widget* self) {
	C_设置视口* box = (C_设置视口*)self;


	S_2D顶点属性 属性 = { {1,1}, {0,0}, 1, 3 };
	//属性.m_偏移 = {0, self->m_扩展视口属性->m_中心.y};
	//属性.m_间隔 = uint32(box->m_Size.x)<<16 | uint32(box->m_Size.y);
	f_vg_set属性(self->m_画布, box->m_背景绘制.m_属性, 属性);

	属性 = { {1,1}, {0,0}, 1, 3 };
	//属性.m_渐变数量 = box->m_GPU内存分配维度大小.x;
	属性.m_间隔 = 3;
	f_vg_set属性(self->m_画布, g网格线绘制属性, 属性);


	box->m_背景绘制.f_alloc(1);
	mat3X2 mat;
	mat.row0 = {};
	mat.row1 = self->m_Size;
	mat.row2 = {};
	box->m_背景绘制.f_set单色填充(0, mat, S_UI主题::uic_面板背景, S_Widget预设图元::m_矩形);


}



C_设置视口::C_设置视口(S_UI创建环境& ctx) : C_Widget(&ctx) {
	mf_布局Fun = on_设置视口布局;
	mf_DrawFun = on_设置视口绘制;
	mf_变换修改 = on_设置视口变换修改;
	mf_鼠标移动 = on_设置视口移动;

	mf_鼠标按键按下 = on_设置视口鼠标按下;
	mf_鼠标按键放开 = on_设置视口鼠标放开;

	m_是否未绘制 = true;
	g当前操作2D编辑视口 = this;

	m_属性栏 = nullptr;



	m_设置类型 = f_alloc_EnumProp(nullptr, {
		{L"界面", "界面"},
		{L"操作", "点击"},
		{L"文件", "文件"},
		{L"编译", "芯片"},
	}, L"设置面板", 1);
	m_设置类型.m_私有 = true;
	m_设置类型.m_UI->m_部件 = this;
	m_设置类型.m_UI->m_Update = on_页面切换_Update;


	mui_切换导航 = f_ui_创建枚举选择框(ctx, m_设置类型, E_方向::e_横向);
	mui_切换导航->m_是否延展 = { true, false };
	mui_切换导航->f_setSize({ 25,25 });
	f_添加子组件(mui_切换导航);
	f_ui_set枚举选择框图标和文字是否绘制(mui_切换导航, true, true);


	
	m_界面设置面板 = new C_界面设置面板(ctx);
	m_操作设置面板 = new C_操作设置面板(ctx);
	m_文件设置面板 = new C_文件设置面板(ctx);
	m_编译设置面板 = new C_编译设置面板(ctx);
	mui_滚动框 = f_ui_创建滚动框(ctx, false, true, m_操作设置面板);
	mui_滚动框->m_是否延展 = { true, true };
	mui_滚动框->mf_鼠标滚轮 = nullptr;
	mui_滚动框->f_setSize({1024,512});
	//mui_滚动框->mf_布局Fun = on_视口滚动框布局;
	f_添加子组件(mui_滚动框);



}

C_设置视口::~C_设置视口() {
	f_prop_Release(m_设置类型);
}

C_Widget* f_工作区页面_设置视口(S_UI创建环境& ctx) {
	C_设置视口* ui设置视口 = new C_设置视口(ctx);

	//f_widget_开启视口属性(ui设置视口);
	//ui设置视口->m_扩展视口属性->m_大小 = { 2048, 2048 };
	//ui设置视口->m_属性栏 = f_创建属性边栏(ctx);

	auto* 滚动窗口 = f_ui_创建滚动框(ctx, true, true, ui设置视口);
	滚动窗口->mf_鼠标滚轮 = nullptr;

	C_设置视口::g当前操作2D编辑视口 = ui设置视口;

	//S_鼠标键位映射* 鼠标键位 = f_widget_init_鼠标键位映射();
	//鼠标键位 = f_widget_init_鼠标键位映射();
	//鼠标键位->event = f_工作区_缩放页面视口;
	//鼠标键位->鼠标按键[0] = E_Input_鼠标按键::e_滚轮;
	//鼠标键位->鼠标状态[0] = DEF_按键状态_按下 | DEF_按键状态_按下中;
	//鼠标键位->发生组件 = ui设置视口;
	//f_ui_注册鼠标快捷键(滚动窗口, (S_键位映射*)鼠标键位);

	return 滚动窗口;
}





static void on_快捷键编辑框布局(C_Widget* self, C_Widget** w, uint32 num) {
	float32 高度 = 30;
	vec2 大小 = { self->m_Size.x - 60, 高度 };

	//if (self->m_父部件) {
		//f_布局_扩展视口纵向滚动(self->m_父部件, self, C_Widget::g_鼠标.lZ * 40);
	//}
	float32 y = self->m_扩展视口属性->m_中心.y;

	for (uint32 i = 0; i < num; ++i) {
		w[i]->f_setPos({ 60, float32(i * (高度 + 4)) + y });
		w[i]->f_setSize(大小);
	}

	self->f_setW(self->m_父部件->m_Size.x);
	self->f_setH(DEF_Max(num * (高度 + 4), self->m_父部件->m_Size.y));
	

	f_widget_单个部件裁剪(self, { self->m_Pos.x, self->m_Pos.y, self->m_父部件->m_Size.x, self->m_父部件->m_Size.y }, self->m_父部件->m_GlobalLoc);
}


C_界面设置面板::C_界面设置面板(S_UI创建环境& ctx) {
	f_setSize({ 512,512 });
	f_widget_开启视口属性(this);
	m_扩展视口属性->m_中心 = {};
}

C_界面设置面板::~C_界面设置面板() {
	
}

C_操作设置面板::C_操作设置面板(S_UI创建环境& ctx) {
	f_widget_开启视口属性(this);
	m_扩展视口属性->m_中心 = {};
	f_setSize({ 1024, 1024 });

	for (auto& e : C_键位映射配置::g_全局键位映射容器) {
		auto* key1 = f_ui_创建快捷键编辑框(ctx);
		f_添加子组件(key1);

		f_ui_快捷键编辑框绑定属性(key1, e.second);
	}


	mf_布局Fun = on_快捷键编辑框布局;
	mf_鼠标滚轮 = on_视口鼠标滚轮;
}

C_操作设置面板::~C_操作设置面板()
{
}

C_文件设置面板::C_文件设置面板(S_UI创建环境& ctx) {
	f_setSize({ 512,512 });
	f_widget_开启视口属性(this);
	m_扩展视口属性->m_中心 = {};
}

C_文件设置面板::~C_文件设置面板()
{
}

C_编译设置面板::C_编译设置面板(S_UI创建环境& ctx) {
	f_setSize({ 512,512 });
	f_widget_开启视口属性(this);
	m_扩展视口属性->m_中心 = {};
}

C_编译设置面板::~C_编译设置面板()
{
}
